package com.nav.backend.controller;

import com.nav.backend.pojo.CollectionPath;
import com.nav.backend.pojo.TreeLine;
import com.nav.backend.pojo.TreeLineSearch;
import com.nav.backend.pojo.User;
import com.nav.backend.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * MapController 地图控制器类，处理与地图相关的请求
 *
 * @author xiukira
 */
@RestController
@RequestMapping("/map")
public class MapController {
    @Autowired
    private MapService mapService;

    /**
     * 林荫道搜索
     *
     * @return 林荫道搜索结果列表
     */
    @PostMapping("/searchBoulevard")
    public ResponseEntity<List<TreeLine>> searchBoulevard(@RequestBody TreeLineSearch treeLineSearch) {
        System.out.println("调用林荫道搜索接口成功");
        //添加林荫道搜索历史记录
        //先查重
        System.out.println("1");
        TreeLine treeLine = mapService.checkTreeLineSearch(treeLineSearch.getTree_line_name(),treeLineSearch.getAccount());
        System.out.println("2");
        if(treeLine == null) {
            mapService.addTreeLineSearch(treeLineSearch.getTree_line_name(), treeLineSearch.getAccount());
        }
        //模糊搜索
        String treeLineName = "%" + treeLineSearch.getTree_line_name() + "%";
        List<TreeLine> treeLines = mapService.searchBoulevard(treeLineName);
        boolean noResults = treeLines.isEmpty();
        if (noResults) {
            System.out.println("林荫道名不存在");
            return ResponseEntity.status(409).body(treeLines);
        } else {
            System.out.println("林荫道名存在");
            for (TreeLine cc : treeLines) {
                System.out.println(cc.getTree_line_id());
                System.out.println(cc.getTree_line_name());
            }
            return ResponseEntity.status(200).body(treeLines);
        }
    }

    /**
     * 获取所有林荫道
     *
     * @return 所有林荫道列表
     */
    @GetMapping("/searchBoulevard/listAll")
    public List<TreeLine> searchBoulevardListAll() {
        System.out.println("调用获取所有林荫道成功");
        List<TreeLine> treeLines = mapService.searchBoulevardListAll();
        return treeLines;
    }
}
